package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.app3.Utli.DateUtil;

import java.util.List;

public class textActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText text;
    private ImageView return1,inner,delete;
    private DBHelper dbHelper;

    private List<User> userList;

    private MyAdapter myAdapter;

   // private ListView lv_show;

    int postion;
    private View activity_text;
    private Bundle bundle;
    private String name;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

        text = findViewById(R.id.editText1);
        return1 = findViewById(R.id.return1);
        inner = findViewById(R.id.inner);
        delete = findViewById(R.id.delete);

        text.setOnClickListener(this);
        return1.setOnClickListener(this);
        inner.setOnClickListener(this);
        delete.setOnClickListener(this);

        //创建数据库对象
        dbHelper = new DBHelper(getApplicationContext(), "user.db", null, 1);


        bundle = getIntent().getExtras();
        if (bundle != null) {
            int dd = bundle.getInt("dd");
            name = bundle.getString("name");
            id = bundle.getString("id");
            postion = dd;


            text.setText(name);
        }


    }

    @Override
    public void onClick(View v) {

        //添加
        if (v.getId() == R.id.inner) {

            if (bundle != null) {

                String s = text.getText().toString();

                User user = dbHelper.get(s);

                if (user.getName() == null) {
                    if (dbHelper.update(id, s, DateUtil.getNowTime())) {
                        showToast("修改成功");

                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);

                    } else {
                        showToast("修改失败");
                    }
                } else {
                    showToast("该信息已存在");
                }



            } else {
                //  et_phone=dialogView.findViewById(R.i.et_pwd);
                String name=text.getText().toString();
                //  String phone=et_phone.getText().toString();

                //根据名字进行查询数据
                User user=dbHelper.get(name);
                //判断当前用户名是否存在
                if (user.getName()==null){
                    //不存在的话进行添加操作

                    if (dbHelper.insert(name, DateUtil.getNowTime())){
                        showToast("添加成功");
                        //添加完成后重新查询
                        // updateListView();

                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }else{
                        showToast("添加失败");
                    }
                }else {
                    showToast("该信息已存在");
                }
            }


        } else if (v.getId()==R.id.return1) {

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
        }

    }


    public void showToast(String msg){
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }
}